package com.easy.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.bean.SysRole;
import com.easy.common.service.IRoleService;
import com.easy.common.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("role")
@RestController
@CrossOrigin
public class RoleController {
    @Autowired
    IRoleService roleService;

    @GetMapping("{id}")
    public ResultData getByID(@PathVariable  Integer id){
        SysRole role= roleService.getByID(id);
        return ResultData.success(role);
    }
    @GetMapping("list")
    public ResultData getAllRole(SysRole role){
        List<SysRole> list= roleService.getList(role);
        return ResultData.success(list);
    }

    @GetMapping("page")
    public ResultData getByPage(Page page , SysRole role){
        IPage<SysRole> list= roleService.getListByPage(page,role);
        return ResultData.success(list);
    }
    @PostMapping("/")
    public ResultData addRole(@RequestBody  SysRole role){
        SysRole newrole=roleService.insert(role);
        return ResultData.success(newrole);
    }
    @DeleteMapping("/{id}")
    public ResultData deleteRole(Integer id){
        roleService.delete(id);
        return ResultData.success();
    }
    @PutMapping("/")
    public ResultData editRole(@RequestBody  SysRole role){
        SysRole newRole=roleService.update(role);
        return ResultData.success(newRole);
    }
    @PutMapping("/assign")
    public ResultData assignPermission(@RequestBody  SysRole role){
        SysRole newRole=roleService.assignPermission(role);
        return ResultData.success(newRole);
    }

}
